package com.example.demo.controller;

import com.example.demo.model.RestResponseBo;
import com.example.demo.model.StaticFields;
import com.example.demo.model.dto.UserDto;
import com.example.demo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    @ResponseBody
    @PostMapping("login")
    public RestResponseBo doLogin(@RequestParam String username,
                                  @RequestParam String password,
                                  HttpServletRequest request){

        UserDto userDto = loginService.login(username,password);
        if (userDto==null){
            return RestResponseBo.fail("账号或密码错误");
        }else {
            request.getSession().setAttribute(StaticFields.LOGIN_SESSION_KEY,userDto);
            return RestResponseBo.ok();
        }
    }




}
